package com.base.caselib;

import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.caselib.bean.FunctionEntity;
import com.base.caselib.databinding.ActivityDucumentBinding;
import com.base.caselib.presenter.DocumentPresenter;
import com.base.caselib.view.DocumentView;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.base.adapter.BaseAdapter;
import com.base.commonlib.base.adapter.BaseHolder;
import com.base.commonlib.bean.Partner;
import com.base.commonlib.recy.utils.CustomDivider;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by weikailiang on 2019/12/10.
 */
@Route(path = "/document/list")
public class DocumentActivity extends CBaseBarMvpActivity<DocumentPresenter> implements DocumentView{
    private ActivityDucumentBinding mBinding;
    private ArrayList<FunctionEntity> mData = new ArrayList<>();
    private BaseAdapter<FunctionEntity> mAdapter;
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_ducument;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityDucumentBinding) mRootBinding;
        mPresenter = new DocumentPresenter(this, this);
        mPresenter.start();
        setBackNavigation();
        setBarTitle("文书打印");
    }

    @Override
    protected void start() {
        //日常工作
        mData.add(new FunctionEntity("/power/matter", "ic_todo", "权利义务告知书"));


        mAdapter = new BaseAdapter<FunctionEntity>(mData,mContext) {
            @Override
            public void convert(BaseHolder holder, FunctionEntity functionEntity, boolean isHeader, boolean isBottom) {
                TextView tv = holder.getView(R.id.text);
                tv.setText(functionEntity.getDes());
                ImageView iv = holder.getView(R.id.img);
                int drawable = getResources().getIdentifier(functionEntity.getPicName(), "drawable", mContext.getPackageName());
                iv.setBackgroundResource(drawable);
            }
        };
        mAdapter.putNormalType(R.layout.item_document);
        mBinding.recycler.setLayoutManager(new LinearLayoutManager(mContext));
        mBinding.recycler.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
                FunctionEntity functionEntity = mData.get(dataPosition);
                if (functionEntity.getType()==1){
                    try {
                        List<Partner> partner = BaseApp.getPartners();
                        if (partner == null||partner.size()==0){
                            ARouter.getInstance().build("/mail/ajpartener").navigation();
                            return;
                        }
                    } catch (Exception e) {
                        showMes("搭档异常，请退出重新登录");
                        return;
                    }
                }
                if (!TextUtils.isEmpty(functionEntity.getClazz())){
                    ARouter.getInstance().build(functionEntity.getClazz()).navigation();
                }else {
                    showMes("待开放");
                }
            }
        });


    }
}
